package com.application.fabric.水印;

import com.application.fabric.水印.添加和提取水印.WatermarkExtractor;
import org.junit.jupiter.api.Test;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class TextWaterMark {
    public void addTextWaterMark(BufferedImage targetImg, Color textColor, int fontSize, String text, String outPath) {
        try {
            int width = targetImg.getWidth(); //图片宽
            int height = targetImg.getHeight(); //图片高
            BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(targetImg, 0, 0, width, height, null);
            g.setColor(textColor); //水印颜色
            g.setFont(new Font("微软雅黑", Font.ITALIC, fontSize));
            // 水印内容放置在右下角
            int x = width - (text.length() + 1) * fontSize;
            int y = height- fontSize * 2;
            g.drawString(text, x, y);
            FileOutputStream outImgStream = new FileOutputStream(outPath);
            ImageIO.write(bufferedImage, "jpg", outImgStream);
            outImgStream.flush();
            outImgStream.close();
            g.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void addTextTest() throws IOException {
        File file = new File("src/test/java/com/application/fabric/水印/test1.jpg");
        BufferedImage image = ImageIO.read(file);
        addTextWaterMark(image, Color.RED, 80, "ceshi", "src/test/java/com/application/fabric/水印/test2.jpg");

    }
    @Test
    public void getText() throws Exception{
        //        提取水印测试
//        读取图片
        // 获取原图片对象
        BufferedImage originalImage = ImageIO.read(new File("src/test/java/com/application/fabric/水印/test2.jpg"));

        // 将原图片对象转换为像素数组
        int[] pixels = originalImage.getRGB(0, 0, originalImage.getWidth(), originalImage.getHeight(), null, 0, originalImage.getWidth());

        // 使用水印提取工具类提取水印图像
        WatermarkExtractor extractor = new WatermarkExtractor();
        BufferedImage watermarkImage = extractor.extract(pixels, originalImage.getWidth(), originalImage.getHeight());
// 将提取出的水印图像显示出来
//        JFrame frame = new JFrame("Watermark Extractor");
//        frame.getContentPane().setLayout(new FlowLayout());
//        frame.getContentPane().add(new JLabel(new ImageIcon(watermarkImage)));
//        frame.pack();
//        frame.setVisible(true);


//        WatermarkExtractor w = new WatermarkExtractor();
//        BufferedImage res = w.extract(pixels,originalImage.getWidth(),originalImage.getHeight());
        System.out.println(watermarkImage.getData());
        System.out.println(watermarkImage);
    }

    @Test
    public void t() throws Exception{
        File file = new File("src/test/java/com/application/fabric/水印/test1.jpg");
        BufferedImage image = ImageIO.read(file);
        addTextWaterMark(image, Color.RED, 80, "ceshi", "src/test/java/com/application/fabric/水印/test2.jpg");

        getText2(image,80,"ceshi");
    }



    public void getText2(BufferedImage targetImg,int fontSize, String text) throws Exception{
//        BufferedImage image = ImageIO.read(file);
        int width = targetImg.getWidth(); //图片宽
        int height = targetImg.getHeight(); //图片高
        // 水印内容放置在右下角
        int x = width - (text.length() + 1) * fontSize;
        int y = height- fontSize * 2;
        int watermark = targetImg.getRGB(x, y); // assuming the watermark is at (0, 0)
//        image.getRGB();
        System.out.println("Watermark: " + watermark);


        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < targetImg.getWidth(); i++) {
            for (int j = 0; j < targetImg.getHeight(); j++) {
                sb.append(Integer.toHexString(targetImg.getRGB(i, j)));
            }
        }
        System.out.println("Watermark text: " + sb);
    }
}
